<?php
/**
 * Zend Image Manipulation Module
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.

 * @category   Zimm
 * @package    Zimm
 * @copyright  Copyright (c) 2012 Kristoffer Ingemansson
 * @license    New BSD License
 * @version    $Id: Sharpen.php 4 2012-04-14 12:59:16Z kristoffer.ingemansson@gmail.com $
 */

class Zimm_Model_DbTable_Instruction_Sharpen extends Zimm_Model_DbTable_Instruction_Abstract
{
    /**
     * Sharpen image via imageconvolution.
     *
     * @param resource $image GD resource
     * @param string $savePath Target save path
     * @param array $params Instruction arguments
     * @return boolean Indicates if image was altered
     */
    protected function _process(&$image, $savePath, $params)
    {
        if (isset($params['amount'])) {
            $corner = -1.2;
            $border = -1;
            $x = $params['amount'];
            $x = 500 - min(500, max(1, $x));
            $max = 1.4*(abs(4*$corner) + abs(4*$border));
            $min = 3*$max;
            $y = ($x/500 * ($min - $max)) + $max;

            $sharpenMatrix = array(
                array($corner, $border, $corner),
                array($border, $y, $border),
                array($corner, $border, $corner),
            );

            $divisor = array_sum(array_map('array_sum', $sharpenMatrix));
            $offset = 0;

            imageconvolution($image, $sharpenMatrix, $divisor, $offset);
            return true;
        }
        return false;
    }
}